package com.qiku.controller;

import com.github.pagehelper.PageInfo;
import com.qiku.domain.PromotionAd;
import com.qiku.domain.ResponseResult;
import com.qiku.service.PromotionAdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/PromotionAd")
public class PromotionAdController {
    @Autowired
    private PromotionAdService promotionAdService;

    @PostMapping("/PromotionAdUpload")
    public ResponseResult PromotionAdUpload(@RequestParam("file") MultipartFile file, HttpServletRequest request) {
        ResponseResult result = null;
        try {
            if (file.isEmpty()){
                throw new RuntimeException("未找到图片信息...");
            }
            //项目部署路径
            String realPath = request.getServletContext().getRealPath("/");
            //项目名称
            String projectName = request.getContextPath().substring(1);
            String requestPath = realPath.substring(0, realPath.indexOf(projectName));
            String uploadPath = requestPath + "/upload/";
            //获取图片名称
            String imgName = file.getOriginalFilename();
            //修改为唯一的名称
            String newImgName = System.currentTimeMillis() + imgName;
            File filePath = new File(uploadPath,newImgName);
            file.transferTo(filePath);
            Map<Object, Object> map = new HashMap<>();
            map.put("fileName", newImgName);
            map.put("filePath", uploadPath + newImgName);
            result = new ResponseResult(true, 200, "上传成功~", map);
            return result;
        } catch (IOException e) {
            e.printStackTrace();
            result = new ResponseResult(false, 500, "发生异常~", null);
            return result;
        }

    }

    /**
     * 添加&修改广告位
     * @param promotionAd
     * @return
     */
    @PostMapping("/saveOrUpdatePromotionAd")
    public ResponseResult saveOrUpdatePromotionAd(@RequestBody PromotionAd promotionAd){
        ResponseResult result = null;
        try{
            if (null != promotionAd.getId() && 0 < promotionAd.getId()){
                promotionAdService.updatePromotionAd(promotionAd);
                result = new ResponseResult(true, 200, "修改成功~", null);
                return result;
            } else {
                promotionAdService.savePromotionAd(promotionAd);
                result = new ResponseResult(true, 200, "保存成功~", null);
                return result;
            }
        } catch (Exception e){
            e.printStackTrace();
            result = new ResponseResult(false, 500, "发生异常~", null);
            return result;
        }
    }

    /**
     * 接收广告ID,返回广告详细信息
     * @param id
     * @return
     */
    @GetMapping("findPromotionAdById")
    public ResponseResult findPromotionAdById(@RequestParam Integer id){
        ResponseResult result = null;
        PromotionAd promotionAd = promotionAdService.findPromotionAdById(id);
        if (null != promotionAd){
            result = new ResponseResult(true, 200, "响应成功~", promotionAd);
            return result;
        } else {
            result = new ResponseResult(false, 500, "查询为空~", null);
            return result;
        }
    }

    /**
     * 修改广告上下线状态
     * @param id
     * @param status
     * @return
     */
    @PostMapping("/updatePromotionAdStatus")
    public ResponseResult updatePromotionAdStatus(@RequestParam Integer id,@RequestParam int status){
        ResponseResult result = null;
        try{
            promotionAdService.updatePromotionAdStatus(id,status);
            Map<String, Integer> map = new HashMap<>();
            map.put("status",status);
            result = new ResponseResult(true, 200, "响应成功~", map);
            return result;
        } catch (Exception e){
            e.printStackTrace();
            result = new ResponseResult(false, 500, "发生错误~", null);
            return result;
        }
    }

    /**
     * 分页获取广告列表数据
     * @param currentPage
     * @param pageSize
     * @return
     */
    @GetMapping("/findAllPromotionAd")
    public ResponseResult findAllPromotionAdByPage(Integer currentPage,Integer pageSize){
        ResponseResult result = null;
        PageInfo<PromotionAd> allAdByPage = promotionAdService.findAllAdByPage(currentPage, pageSize);
        if (null != allAdByPage){
            result = new ResponseResult(true, 200, "响应成功~", allAdByPage);
            return result;
        } else {
            result = new ResponseResult(false, 500, "查询为空~", null);
            return result;
        }
    }
}
